package com.ys.timer;

import com.ys.entity.storage.TccAllocate;
import com.ys.service.storage.TccDistributeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.util.List;


@Controller
public class TimerTccDistributeDaily {
	private static final Logger logger = LoggerFactory.getLogger(TimerTccDistributeDaily.class);

    @Autowired
    private TccDistributeService tccDistributeService;


	/**
	 * 隔天凌晨2点自动分配
	 */
	@Scheduled(cron = "0 0 2 * * ?")
	public void automaticAllocation
			() {
		logger.info("自动生成分配单开始--------------------");
		//预约调拨在前的先分配 还存在未分配
		List<TccAllocate> tccAllocateList = new TccAllocate().setEffectStock(1)
				.where("(SELECT count(1) From TCC_AllocateDetail B WHERE A.bid = B.bidAllocate AND B.StateDistribute = 0 AND B.DistributeNum >= 0 AND B.MayDistributeNum > 0) >0")
				.setOrderby("A.CreateTime DESC")
				.queryList();
		for(TccAllocate tccAllocate : tccAllocateList){
			tccDistributeService.automaticAllocation(tccAllocate);
		}
		logger.info("自动生成分配单结束--------------------");
	}

}
